import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:wechat_assets_picker_plus/wechat_assets_picker_plus.dart';
import '../../../package_event.dart';
import 'package:photo_manager/photo_manager.dart';

class MediaInformationView extends StatelessWidget {
  // title
  final String title;

  // 资源列表
  final RxList<AssetModel> assets;
  //
  final String? assetsContent;

  // 是否支持编辑
  final RxBool isEdit;

  // 按钮是否高亮
  final RxBool allowedSubmit;

  // 内容
  final RxString? content;

  // 添加资源图片
  final Function()? addAssetOnTap;

  // 提交
  final Function()? submitOnTap;

  final int? maxCount;

  // 回调TextFeild的值
  final Function(String)? callbackTextFeildValue;

  MediaInformationView({
    required this.title,
    required this.assets,
    required this.isEdit,
    required this.allowedSubmit,
    this.assetsContent,
    this.content,
    this.addAssetOnTap,
    this.submitOnTap,
    this.maxCount = 9,
    this.callbackTextFeildValue,
  }) {
    if (!assets.any((element) => element.isAdd) &&
        isEdit.value &&
        assets.length < (maxCount ?? 9)) {
      assets.add(AssetModel.add);
    }
  }

  @override
  Widget build(BuildContext context) {
    bool isEmptyContent = content == null || content?.value.isEmpty == true;
    List<String> urls = assets.map((element) => element.src).toList();
    return Obx(
      () => Padding(
        padding: EdgeInsets.only(
          left: CONTENT_MARGIN,
          right: CONTENT_MARGIN,
          top: CONTENT_MARGIN,
          bottom: CONTENT_MARGIN / 2,
        ),
        child: ClipRRect(
          borderRadius: const BorderRadius.all(Radius.circular(8)),
          child: Container(
            padding: EdgeInsets.all(CONTENT_MARGIN),
            color: LxColors.borderBackgroud,
            width: double.infinity,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                LxText(
                  title,
                  color: Colors.white.withOpacity(0.6),
                  fontWeightEnum: LxFontWeightEnum.bodyMedium,
                ),
                Visibility(
                  visible: !isEmptyContent,
                  child: Column(
                    children: [
                      SizedBox(
                        height: CONTENT_MARGIN,
                      ),
                      LxText(
                        content?.value ?? "",
                        color: Colors.white,
                        fontWeightEnum: LxFontWeightEnum.bodyMedium,
                      ),
                    ],
                  ),
                ),
                Visibility(
                    visible: isEdit.value,
                    replacement: MediaContentDetailView(
                      srcUrls: urls,
                      onTap: (url) {
                        AssetModel model = AssetModel.fromMap({})..src = url;
                        if (model.type == AssetType.video) {
                          PackageEvent.playVideo((p0) {}, param: ["", url]);
                        } else {
                          PackageEvent.playPicture((p0) {},
                              param: urls
                                  .where((e) => !e.endsWith(".mp4"))
                                  .toList());
                        }
                      },
                    ),
                    child: Column(
                      children: [
                        MediaContentEditView(
                          width: context.width - 4 * CONTENT_MARGIN,
                          assetModels: assets,
                          addOnTap: () {
                            if (addAssetOnTap != null) {
                              addAssetOnTap!();
                            }
                          },
                          deleteOnTap: (m) {
                            assets.remove(m);
                            if (!assets.last.isAdd) {
                              assets.add(AssetModel.add);
                            }
                          },
                          previewOnTap: (m) {},
                          itemBackground: const Color(0xFF2B2B2B),
                          isSupportDelete: true,
                        ),
                        EditTextField(
                          callbackTextFeildValue: callbackTextFeildValue,
                        ),
                        SizedBox(height: CONTENT_MARGIN),
                      ],
                    )),
                Visibility(
                  visible: isEdit.value,
                  child: LxButton(
                    title: AppLanguageUtil.to.titleSubmit,
                    buttonStausEnum: allowedSubmit.value ? ButtonStausEnum.normal
                        : ButtonStausEnum.disable,
                    onTap: () {
                      if (submitOnTap != null && allowedSubmit.value == true) {
                        submitOnTap!();
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

// 上传图片/视频下的输入文本，防止被重建
class EditTextField extends StatefulWidget {
  // 回调TextFeild的值
  final Function(String)? callbackTextFeildValue;
  const EditTextField({
    super.key,
    // 回调TextFeild的值
    this.callbackTextFeildValue,
  });

  @override
  EditTextFieldState createState() => EditTextFieldState();
}

class EditTextFieldState extends State<EditTextField> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _controller,
      decoration: InputDecoration(
        labelText: AppLanguageUtil.to.titleEnterAdditionalResponsesPlacedodler,
      ),
      cursorColor: LxColors.theme,
      style: TextStyle(
        fontSize: 14,
        fontWeight: FontWeight.w400,
        color: Colors.white.withOpacity(0.8),
      ),
      onChanged: (value) {
        if (widget.callbackTextFeildValue != null) {
          widget.callbackTextFeildValue!(value);
        }
      },
    );
  }
}
